#include <iostream>
#include <string>
#include <print>


/*
 * 2024/3/27
 * Compilation principle
 * Automata
 * Author: 甘劲博
 * StudentID: 2110551202
 * Class: 21计科5
 *
 */

#define F 0
#define RIGHT 1
#define DOT 2
#define H 3

int state[6][4] = { //状态转移表
        {1, 2, 0, 0},
        {0, 2, 0, 0},
        {0, 2, 3, 5},
        {0, 4, 0, 0},
        {0, 4, 0, 5},
        {0, 0, 0, 0},
};

bool Accept(const std::string &str) {
    int s = 0;
    int len = str.length();
    for (int i = 0; i < len; i++) {
        if (str[i] == '-' || str[i] == '+') {
            if (state[s][F] == 0) return false;
            s = state[s][F];
        } else if (std::isdigit(str[i]) ||
                   (std::isalpha(str[i]) &&
                    ((str[i] >= 'A' && str[i] <= 'F') ||
                     (str[i] >= 'a' && str[i] <= 'f')))) {
            if (state[s][RIGHT] == 0) return false;
            s = state[s][RIGHT];
        } else if (str[i] == '.') {
            if (state[s][DOT] == 0) return false;
            s = state[s][DOT];
        } else if (std::toupper(str[i]) == 'H') {
            if (state[s][H] == 0) return false;
            s = state[s][H];
        } else {
            return false;
        }
    }
    return s == 5;
}

void ShowInfo() {
    std::string str = R"(
 * 2024/3/26
 * Compilation principle
 * Automata
 * Author: 甘劲博
 * StudentID: 2110551202
 * Class: 21计科5)";
    std::println("{}", str);
}

int main() {
    ShowInfo();
    std::string str;
    std::println("Enter string to judge");
    std::println("Enter 'exit' to exit");
    while (std::cin >> str) {
        if (str == "exit")
            return 0;
        if (Accept(str))
            std::println("Accept!");
        else
            std::println("Not Accept!");
    }
    return 0;
}